package com.cjyd.hrjb.utils;

import com.cjyd.hrjb.model.dto.UserDto;
import com.cjyd.hrjb.model.entity.User;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Created fei wo by 2020/4/6
 */
public class XetUserUtil {
    /**
     * 构建小鹅通用户注册参数
     * @param accessToken access token
     * @param userDto 用户信息
     * @param ignoreFields 忽略字段
     * @return {@link Map<String,Object>}
     * @throws IllegalAccessException 字段访问异常
     */
    public static Map<String,Object> buildRegUserParams(String accessToken, UserDto userDto, String ...ignoreFields) throws IllegalAccessException {
        Map<String,Object> params = new HashMap<>();
        params.put("access_token", accessToken);
        Map<String,Object> data = new HashMap<>();
        params.put("data", data);
        Field[] fields = userDto.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (!Arrays.asList(ignoreFields).contains(field.getName())) {
                if (field.get(userDto) != null) {
                    JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
                    if (jsonProperty != null) {
                        data.put(jsonProperty.value(), field.get(userDto));
                    }else {
                        data.put(field.getName(), field.get(userDto));
                    }
                }
            }
        }
        return params;
    }

    public static Map<String,Object> buildGetUserInfoParams(String accessToken, String userId,String ...ignoreFields) {
        Map<String,Object> params = new HashMap<>();
        params.put("access_token", accessToken);
        params.put("user_id", userId);
        Map<String,Object> data = new HashMap<>();
        List<String> fieldNames = new ArrayList<>();
        Field[] fields = User.class.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (!Arrays.asList(ignoreFields).contains(field.getName())) {
                JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
                if (jsonProperty != null) {
                    fieldNames.add(jsonProperty.value());
                }else {
                    fieldNames.add(field.getName());
                }
            }
        }
        data.put("field_list", fieldNames);
        params.put("data", data);
        return params;
    }
}
